import {
  dashboardGadgetList,
  slicerGadgetTypeList,
  defaultGadgetSettingList,
  noFilterGadgetType
} from '@/utils/data-json'
import { v4 } from 'uuid'

const useGadget = (type) => {
  return dashboardGadgetList.find(i => i.key === type) || {}
}

export const useGadgets = (type) => {
  return dashboardGadgetList.filter(i => i.key === type)
}

export const useGadgetSetting = (type) => {
  return defaultGadgetSettingList.find(e => e.gadgetType === type).gadgetSetting || {}
}

export const isSlicer = (gadgetType) => slicerGadgetTypeList.some(key => key === gadgetType)
export const isWidget = (gadgetType) => noFilterGadgetType.some(key => key === gadgetType)

/**
 * pending
 * success
 * failing
 * blocking
 */

export const setGadgetStatus = (list, name) => {
  return list.map(element => {
    if (element.com === 'pageheadergadget') {
      element.name = name
    }

    if (isSlicer(element.com)) {
      return {
        ...element,
        type: 'slicer',
        state: 'pending',
        setState (state) {
          this.state = state
        }
      }
    }
    return {
      ...element,
      type: 'widget',
      state: 'blocking',
      setState (state) {
        this.state = state
      }
    }
  })
}

export const getGadget = (widget) => {
  const { gadgetType, id, name, integrationList, businessType, gadgetId, pageFrom } = widget || {}
  const setting = useGadget(gadgetType)
  return {
    'x': setting.x || 0,
    'y': setting.y || 0,
    'w': pageFrom === 'reportLayout' ? 4 : (setting.w || 6),
    'h': pageFrom === 'reportLayout' ? isSlicer(setting.key) ? 1 : 4 : (setting.h || 16),
    i: gadgetId || v4(),
    com: setting.key,
    id,
    name,
    integrationList,
    businessType
  }
}

export default useGadget
